import path from 'path'
import ts from 'rollup-plugin-typescript2'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'

const name = 'frontend-dsl'
const rootDir = __dirname
const resolve = p => path.resolve(rootDir, p)

const outputConfig = {
  // 'esm-bundler': {
  //   file: resolve(`dist/${name}.esm-bundler.js`),
  //   format: 'es'
  // },
  'common-bundler': {
    file: resolve(`dist/${name}.common-bundler.js`),
    format: 'commonjs'
  }
}

function createConfig (output) {
  // 暂时不生成类型定义文件
  const shouldEmitDeclarations = false

  const tsPlugin = ts({
    check: false,
    tsconfig: path.resolve(__dirname, 'tsconfig.json'),
    cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'),
    tsconfigOverride: {
      compilerOptions: {
        sourceMap: false, // output.sourcemap,
        declaration: shouldEmitDeclarations,
        declarationMap: shouldEmitDeclarations
      },
      exclude: ['**/__tests__', 'test-dts']
    }
  })

  output.sourcemap = false
  output.externalLiveBindings = false

  return {
    input: resolve('src/index.ts'),
    output,
    plugins: [
      tsPlugin,
      nodeResolve(),
      commonjs(),
      json()
    ],
    treeshake: {
      moduleSideEffects: false
    },
    external: [
      'path', 'fs', 'child_process', 'events', 'util', 'os', 'assert',
      'tty', 'stream', 'module', 'buffer', 'fs/promises'
    ]
  }
}

export default [
  createConfig(outputConfig['common-bundler'])
]